
import { Form, Modal, Button } from "antd";
import React, { useImperativeHandle } from "react";
import { useState } from "react";
import ModalContent from "./condtionModal";
import { getLabelOrValue } from "../../utils/commonJS";
import { useRef } from "react";
import dayjs from "dayjs";
// 组装城市数据（查询大屏数据的时候要用）
const getStr = (regList) => {
    let tempList = [];
    if (regList.length > 0) {
        for (var i = 0; i < regList.length; i++) {
            var arr = regList[i];
            if (arr.length === 1) {
                tempList = [...tempList, ...arr]
            } else {

                tempList = [...tempList, ...(arr.slice(1, arr.length))]
            }
        }
    }
    return tempList;
}

/**
 * 
 * @param {*} props 
 * @returns 
 * @description 查询条件弹窗组件
 */
const ModalConditionComponent = ({
    show = false,
    setShow,
    setParams,
    prentParams
}) => {

    // form表单
    const [form] = Form.useForm();
    const [loading, setLoading] = useState(false)
    const publishRef = useRef();


    // 触发表单校验
    const onSaveData = () => {
        form.validateFields().then(res => {
            console.log("校验表单", res)
            onFinish(res);
        }).catch((err) => {
            console.log("表单校验失败", err)
        })

    }


    const onFinish = (params) => {
        const { time = [],

            city = [],
            engPlantLabelInValue = [],//主机厂
            salesStoreNameInCode = [],//经销店名称
            salesBrandLabelInValue = [],//经销店品牌 
            dealerLabelInValue = [],//经销商集团 
            comLabelInValue = [],//机构
            teamLabelInValue = [],//团队
            businLabelInValue = [],//业务经理
            useLabelInValue = [],//使用性质
            catTypeLabelInValue = [],//车系
            serviceLabelInValue = [],
            energyTypeLabelInValue = [],
            subsidyFlag = "",
            // uticService = "0",
        } = params;
        const object = {
            startTime: dayjs(time[0]).format("YYYYMM"),//开始时间
            endTime: dayjs(time[1]).format("YYYYMM"),//结束时间
            factoryCode: getLabelOrValue('value', engPlantLabelInValue, 2), //主机厂
            factoryName: getLabelOrValue('label', engPlantLabelInValue, 2),
            insuredCode: getLabelOrValue('value', salesStoreNameInCode, 2),//经销店名称
            insuredBrand: getLabelOrValue('value', salesBrandLabelInValue, 2),//经销店品牌 code
            insuredBrandName: getLabelOrValue('label', salesBrandLabelInValue, 2), //经销店品牌 name
            insuredGroup: getLabelOrValue('value', dealerLabelInValue, 2),//经销店集团
            insuredGroupName: getLabelOrValue('label', dealerLabelInValue, 2),//经销店集团
            cityCode: getStr(city), //行政区 城市 

            secComCode: getLabelOrValue('value', comLabelInValue, 2),//二级机构
            secComName: getLabelOrValue('label', comLabelInValue, 2),//二级机构
            salesTeam: getLabelOrValue('value', teamLabelInValue, 2),//团队机构
            salesTeamName: getLabelOrValue('label', teamLabelInValue, 2),//团队名称

            salesName: getLabelOrValue('value', businLabelInValue, 2),//业务经理代码,
            businManagerName: getLabelOrValue('label', businLabelInValue,2),//业务经理名称
            useNatureCode: getLabelOrValue('value', useLabelInValue, 2),//使用性质
            useNatureName: getLabelOrValue('label', useLabelInValue,2),
            serviceNature: getLabelOrValue('value', serviceLabelInValue, 2),//营运性质
            serviceNatureName: getLabelOrValue('label', serviceLabelInValue, 2),//营运性质
            fuelType: getLabelOrValue('value', energyTypeLabelInValue, 2),//能源种类
            fuelTypeName: getLabelOrValue('label', energyTypeLabelInValue,2),

            seriesCode: getLabelOrValue('value', catTypeLabelInValue, 2),
            seriesName: getLabelOrValue('label', catTypeLabelInValue,2),
            subsidyFlag: subsidyFlag,//是否补贴  新增
            // uticService: [uticService]
        }
        console.log("发给后端的数据表单参数：", object)
        setParams({
            ...object,
            "pageIndex": 1,
            "pageSize": 500,
        })
        cancelUpload();
    }



    // 取消上传/关闭上传窗口
    const cancelUpload = () => {
        setShow(false)
    }





    const publishConfig = {
        form, prentParams
    }

    return (
        <>
            <Modal
                open={show}
                title={"筛选条件"}
                centered={true}
                // destroyOnClose={true}
                width={1300}
                style={{ height: 700 }}
                maskClosable={false}
                onCancel={cancelUpload}
                footer={[
                    <Button ghost onClick={() => form.resetFields()} >重置</Button>,
                    <Button ghost loading={loading} type="primary" onClick={() => {
                        onSaveData()
                    }}>
                        查询
                    </Button>
                ]}
            >
                <ModalContent onRef={publishRef}  {...publishConfig} />
            </Modal>
        </>
    )
}

export default ModalConditionComponent;